package com.example.a117.myapplication;

/**
 * Created by a117 on 2018/6/14.
 */

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ClipData;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a117.myapplication.Fields.DiaryColumns;


//继承来自listView
public class LifeDiary extends ListActivity  {
    // 插入一条新纪录
    public static final int MENU_ITEM_INSERT = Menu.FIRST;
    // 编辑内容
    public static final int MENU_ITEM_EDIT = Menu.FIRST + 1;
    public static final int MENU_ITEM_DELETE = Menu.FIRST + 2;
    private static final String[] PROJECTION =
            new String[] { DiaryColumns._ID,
                    DiaryColumns.TITLE, DiaryColumns.CREATED };
private ListView lv;
    //ListView lv = getListView();
    //private TextView tv;
    //private LifeDiaryContentProvider.DatabaseHelper mOpenHelper;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_list);

        //悬浮按钮
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent0 = new Intent(LifeDiary.this,DiaryEditor.class);
                intent0.setAction(DiaryEditor.INSERT_DIARY_ACTION);
                intent0.setData(getIntent().getData());
                startActivity(intent0);

            }
        });

getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?>parent, View view, final int position, long id) {
        AlertDialog.Builder builder=new AlertDialog.Builder(LifeDiary.this);
        builder.setMessage("确认删除？");
        builder.setTitle("提示");

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
Uri uri = ContentUris.withAppendedId(getIntent().getData(),
                 getListView().getItemIdAtPosition(position));

                 getContentResolver().delete(uri, null, null);
                renderListView();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();



        return true;
    }
});
        Intent intent = getIntent();
        if (intent.getData() == null) {
            intent.setData(DiaryColumns.CONTENT_URI);
        }
        Cursor cursor = managedQuery(getIntent().getData(),
                PROJECTION, null,null, DiaryColumns.DEFAULT_SORT_ORDER);
		/*SimpleCursorAdapter允许你绑定一个游标的列到ListView上
		并使用自定义的layout显示每个项目。
		SimpleCursorAdapter的创建，需要传入
		当前的上下文、一个layout资源，一个游标和两个数组
		两个数组：1使用的列的名字，2（相同大小）数组包含View中的资源ID
		用于显示相应列的数据值。*/
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.diary_row, cursor, new String[]
                { DiaryColumns.TITLE,DiaryColumns.CREATED },
                new int[] { R.id.text1,R.id.created });
        setListAdapter(adapter);




    }
    //添加选择菜单
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, MENU_ITEM_INSERT, 0, R.string.menu_insert);
        menu.add(Menu.NONE, MENU_ITEM_DELETE, 0, R.string.menu_delete);
        return true;
    }
    //添加选择菜单的选择事件
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // 插入一条数据
            case MENU_ITEM_INSERT:
                Intent intent0 = new Intent(this, DiaryEditor.class);
                intent0.setAction(DiaryEditor.INSERT_DIARY_ACTION);
                intent0.setData(getIntent().getData());
                startActivity(intent0);
                return true;
            // 编辑当前数据内容通过单击日记名实现
            // 删除当前数据
            case MENU_ITEM_DELETE:
                Uri uri = ContentUris.withAppendedId(getIntent().getData(),
                        getListView().getSelectedItemId());



               getContentResolver().delete(uri, null, null);
                renderListView();
        }
        return super.onOptionsItemSelected(item);
    }
    protected void onListItemClick
            (ListView l, View v, int position, long id) {
        Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
        startActivity(new Intent(DiaryEditor.EDIT_DIARY_ACTION, uri));
    }

    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        renderListView();
    }
    private void renderListView() {
        Cursor cursor = managedQuery(getIntent().getData(), PROJECTION,
                null,null, DiaryColumns.DEFAULT_SORT_ORDER);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.diary_row, cursor, new String[] { DiaryColumns.TITLE,
                DiaryColumns.CREATED }, new int[] { R.id.text1,R.id.created });
        setListAdapter(adapter);
    }




}
